# -*- coding: utf-8 -*-
# @Time    : 2021/3/20 9:42
# @Author  : wenyao
# @Email   : 343292019@qq.com
# @File    : 01.错误与异常.py

#出现异常 程序就终止了
# print("start......")
# f = open("test.txt")
# print("end....")

#NameError
# print(name)

#ZeroDivisionError
# a = 1/0

#缩进错误
 # print("ok")

#IndexError
# a = "abc"
# print(a[4])

#SyntaxError
# if 1>0 ::
#    print("ok")

#AttributeError
# a = "123"
# a.xxx()

#ValueError
# int("abc")

# 1：即使程序出错，也不想让程序终止
# 2：如果出错了，需要特殊处理
#try..except 异常捕获

#一旦发生异常就跳到except去处理了，不会再进行下面的语句执行
#except 可以有多个
#except 捕获的错误与程序触发的错误不一致，程序就会捕获不到
#匹配到第一个except，try里面的程序就退出了，不会向下继续运行
#父类异常在最前面，会吞噬所有的子类异常

# try:
#     print("start try........")
#     a = 1/0
#     print(a)
#     print("end try........")
# except NameError as na:
#     print("name error")
# except ZeroDivisionError as zr:
#     print("zero error")
# except Exception as ex:
#     print("exception error")
#异常基类

# print("end........")

#else结构
#没有发生任何异常时，处理else语句块
# try:
#     print("start try......")
#     a = 1/0
# except Exception:
#     print("exception")
# else:
#     print("else ")

#finally
#不管有没有异常，不管异常有没有被捕获，都执行
# try:
#     print("start try.....")
#     a = [1,2,3]
#     print(a[4])
# except NameError as ie:
#     print("index error")
# else:
#     print("else...")
# finally:
#     print("this is finally")

#raise 手动触发异常
#raise  异常类("说明信息")  / raise 异常类
# try:
#     print("raise error..")
#     raise IndexError("this is test error")
# except IndexError as e:
#     print(e)

#练习
# try:
#     num1 = int(input("请输入一个数"))
#     if num1>100 or num1 <0:
#         raise IndexError("超出范围")
# except ValueError as va:
#     print(va)
# except IndexError as  ie:
#     print(ie)
# else:
#     print("成绩为:",num1)
# print("end......")

#嵌套异常
#即使函数return， 也要执行finally
# def func1():
#     try:
#         a = "hello world"
#         try:
#             int(a)
#         except ValueError as e:
#             print("Value Error")
#             return  "i am value error"
#         finally:
#             print("i am finally")
#         print("*"*20)
#         b = [1,2,3]
#         print(b[4])
#     except IndexError as msg:
#         print("Index Error")
#     finally:
#         print("i am index error finally")

# b = func1()
# print("-"*20)
# print(b)

#try:
#   语句块
#except：
#   语句块

#try：
#   语句块
#finally:
#   语句块

# try:
#     print("abc")
# finally:
#     print("i am finally")